package com.ruoyi.technicianStatistics.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.config.util.address.AddressUtil;
import com.ruoyi.config.util.address.WSSsdrAddress;
import com.ruoyi.config.util.gaode.GaoDeUtil;
import com.ruoyi.other.domain.Region;
import com.ruoyi.other.mapper.RegionMapper;
import com.ruoyi.technician.domain.Technician;
import com.ruoyi.technician.mapper.TechnicianMapper;
import com.ruoyi.technicianStatistics.vo.TechnicianDistribution;
import com.ruoyi.technicianStatistics.vo.TechnicianSexResponseVo;
import com.ruoyi.technicianStatistics.vo.TechnicianStatisticsResponseVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 技师统计Controller
 *
 * @author ruoyi
 * @date 2025-04-19
 */
@RestController
@RequestMapping("/technicianStatistics/technicianStatistics")
public class TechnicianStatisticsController extends BaseController {
    @Resource
    private TechnicianMapper technicianMapper;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    private GaoDeUtil gaoDeUtil;

    /**
     * 新增技师统计
     */
    @Log(title = "技师统计", businessType = BusinessType.INSERT)
    @PostMapping
    public TechnicianStatisticsResponseVo technicianStatistics() {
        TechnicianStatisticsResponseVo responseVo = new TechnicianStatisticsResponseVo();
        TechnicianStatisticsResponseVo technicainData = technicianMapper.selectTechnicainData();

        responseVo.setTechnicianCount(technicainData.getTechnicianCount());

        TechnicianStatisticsResponseVo technicainOnlineData = technicianMapper.selectTechnicainOnlineData();
        responseVo.setTechnicianOnlineCount(technicainOnlineData.getTechnicianOnlineCount());

        responseVo.setTechnicianNotlineCount(technicainData.getTechnicianCount() - technicainOnlineData.getTechnicianOnlineCount());

        List<TechnicianStatisticsResponseVo> technicianStatisticsResponseVos = technicianMapper.selectTechnicianTakeOrderCount();

        responseVo.setTechnicianTakeOrderCount(technicianStatisticsResponseVos.size());

        return responseVo;
    }

    @Log(title = "技师统计", businessType = BusinessType.INSERT)
    @GetMapping
    public List<TechnicianSexResponseVo> technicianSex() {
        List<TechnicianSexResponseVo> responseVoList = new ArrayList<>();
        Integer count = technicianMapper.selectCount();
        if (CheckEmptyUtil.isEmpty(count)) {
            count = 0;
        }
        Integer manCount = technicianMapper.selectCountBySex(0);
        if (CheckEmptyUtil.isEmpty(manCount)) {
            manCount = 0;
        }
        Integer wemanCount = technicianMapper.selectCountBySex(1);
        if (CheckEmptyUtil.isEmpty(wemanCount)) {
            wemanCount = 0;
        }

        TechnicianSexResponseVo manCountVo = new TechnicianSexResponseVo();
        manCountVo.setName("男");
        manCountVo.setValue(manCount);
        if (count == 0) {
            manCountVo.setRate(BigDecimal.ZERO);
        } else {
            BigDecimal divide = new BigDecimal(manCount).divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            manCountVo.setRate(divide.multiply(new BigDecimal(100)));
        }
        responseVoList.add(manCountVo);

        TechnicianSexResponseVo wemanCountVo = new TechnicianSexResponseVo();
        wemanCountVo.setName("女");
        wemanCountVo.setValue(wemanCount);
        if (count == 0) {
            wemanCountVo.setRate(BigDecimal.ZERO);
        } else {
            BigDecimal divide = new BigDecimal(wemanCount).divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            wemanCountVo.setRate(divide.multiply(new BigDecimal(100)));
        }
        responseVoList.add(wemanCountVo);

        return responseVoList;
    }

    @GetMapping("technicianDistribution")
    public List<TechnicianDistribution> technicianDistribution() {
        // 将Map转换为列表，便于返回
        List<TechnicianDistribution> responseVoList = new ArrayList<>();
        //查询所有身份
        List<Region> regions = regionMapper.selectPartner();
        //查询所有技师
        List<Technician> technicians = technicianMapper.selectAllTechnicianList();

        // 使用Map存储省份名称和对应的技师数量
        Map<String, Integer> regionTechnicianCountMap = new HashMap<>();

        // 初始化Map，为每个省份设置初始值为0
        for (Region region : regions) {
            regionTechnicianCountMap.put(region.getName(), 0);
        }
        if (CheckEmptyUtil.isEmpty(technicians)){
            for (Map.Entry<String, Integer> entry : regionTechnicianCountMap.entrySet()) {
                TechnicianDistribution distribution = new TechnicianDistribution();
                distribution.setName(entry.getKey());
                distribution.setCount(entry.getValue());
                responseVoList.add(distribution);
            }
            return responseVoList;
        }
        for (Technician technician : technicians) {
            String address = gaoDeUtil.ReGeoCode(technician.getLongitude() + "," + technician.getLatitude());
            WSSsdrAddress formatAddress = AddressUtil.addressFormat(address);
            String province = formatAddress.getProvince();
            if (regionTechnicianCountMap.containsKey(province)) {
                regionTechnicianCountMap.put(province, regionTechnicianCountMap.get(province) + 1);
            }
        }


        for (Map.Entry<String, Integer> entry : regionTechnicianCountMap.entrySet()) {
            TechnicianDistribution distribution = new TechnicianDistribution();
            distribution.setName(entry.getKey());
            distribution.setCount(entry.getValue());
            responseVoList.add(distribution);
        }

        return responseVoList;
    }


}
